package com.example.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.example.common.MD5;
import com.example.common.TokenUtils;
import com.example.dao.PassWord;
import com.example.entity.HnyyWnPermission;
import com.example.entity.User;
import com.example.exception.CustomerException;
import com.example.mapper.LoginMapper;
import com.example.service.LoginService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class LoginServiceImpl implements LoginService {
    @Autowired
    private LoginMapper loginMapper;

    @Override
    public User login(String userName, String passWord) {
        String passWordMd5 = MD5.getMD5Hash(passWord);
        System.out.println("passWordMd5====>"+passWordMd5);
        User user = loginMapper.login(userName, passWordMd5);
        if (user == null) {
            return null;
        }
        String token = TokenUtils.createToken(user.getComPartyId()+'-'+user.getUserId()+'-'+user.getSaler(), user.getPassWord());
        user.setAccessToken(token);
        System.out.println("token=====>"+token);
        return user;
    }

    @Override
    public User selectById(String userId) {
        User user = loginMapper.selectById(userId);
        return user;
    }

    @Override
    public List<HnyyWnPermission> getPermission(String userId) {
        List<HnyyWnPermission> permission = loginMapper.getPermission(userId);
        return permission;
    }

    @Override
    public Integer forget(PassWord passWord, String userId) {
        String passWordMd5 = MD5.getMD5Hash(passWord.getPassword());
        passWord.setPassword(passWordMd5);
        String newPassWordMd5 = MD5.getMD5Hash(passWord.getNewPassword());
        passWord.setNewPassword(newPassWordMd5);
        System.out.println("1232131"+JSONObject.toJSONString(passWord));
        return loginMapper.forget( passWord.getNewPassword(),passWord.getPassword(), userId);
    }
}
